package com.hyf.glidedemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hyf.glidedemo.R;

/**
 * Created by Administrator on 2017/12/15.
 */

public class GlideRecyclerViewAdapter extends RecyclerView.Adapter<GlideRecyclerViewAdapter.MyRecyclerHolder> {

    private Context mContext;
    private String[] datas = {
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702772&di=fab80ce0835ffe61576b2c3f16191078&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fimage%2Fcrop%253D0%252C0%252C640%252C410%2Fsign%3D003259c1e1c4b74520dbed56f2cc3227%2Fb219ebc4b74543a98841252414178a82b80114fb.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702772&di=e8e00b959750584cef3b0a44bbf14587&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F4afbfbedab64034fe27ca6eba5c379310b551dad.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702772&di=a7e0122105cf86eb8d2a02410068b139&imgtype=0&src=http%3A%2F%2Fe.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F72f082025aafa40fe74dc03ba164034f79f01948.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702771&di=900d30daa0d14ddff76894dcb32539b8&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F622762d0f703918fbbee01195b3d269759eec421.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702920&di=1fe672eeb285f34f8a830338d112e335&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F58ee3d6d55fbb2fbd104f7fe454a20a44623dc2b.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702919&di=27ea3aaa87714bcdb373f5ac172223f1&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F377adab44aed2e73c5560bcb8d01a18b86d6fa9e.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702919&di=78aed3f230a789379a6831f9eace62d3&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fb90e7bec54e736d17027a1ee91504fc2d562691b.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702940&di=fe44cd7af7ddd85bc480b9749c63dd2f&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F0dd7912397dda14487126162b8b7d0a20df486c2.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702940&di=60f3a48901c0b1eef6288119a736201c&imgtype=0&src=http%3A%2F%2Fe.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F0b46f21fbe096b63e2ea43bd06338744eaf8ac6f.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702362&di=b8ea52fdc4a96c22024a7f54a90307a6&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0115f158ad6c38a801219c77b6447c.jpg%40900w_1l_2o_100sh.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338834769&di=a6aae3db5baa2bc46373781afaba1386&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D1656982379%2C2696530929%26fm%3D214%26gp%3D0.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338843748&di=1286579a997a4d95da6e2194bd3b8a3e&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D3156094829%2C1157468990%26fm%3D214%26gp%3D0.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702361&di=aa74d0edf9b67e392ca8005bba3b1f8c&imgtype=0&src=http%3A%2F%2Fimg2.ph.126.net%2FeqslZ6y2jiw6qe1e4dNJAw%3D%3D%2F6631255782187092197.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702360&di=928f396d24f9ea5800ee6c4a91d88c70&imgtype=0&src=http%3A%2F%2Fp4.image.hiapk.com%2Fuploads%2Fallimg%2F140825%2F912-140R5152020.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513338702360&di=3214b6018a5ec02a024f09813da6aeb2&imgtype=0&src=http%3A%2F%2Fimg.tupianzj.com%2Fuploads%2Fallimg%2F160229%2F9-160229111126.jpg"
    };

    private LayoutInflater mLayoutInflater;

    public GlideRecyclerViewAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public MyRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.layout_glide_recycler_item, parent, false);
        return new MyRecyclerHolder(view);
    }

    @Override
    public void onBindViewHolder(MyRecyclerHolder holder, int position) {
        holder.mTextView.setText((position + 1) + "");
        Glide.with(mContext).load(datas[position]).diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().into(holder.mImageView);
    }

    @Override
    public int getItemCount() {
        return datas.length;
    }

    class MyRecyclerHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        ImageView mImageView;

        public MyRecyclerHolder(View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.tv_glide_recycler_item);
            mImageView = itemView.findViewById(R.id.iv_glide_recycler_item);
        }
    }
}
